package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.system.domain.AppUser;
import com.ruoyi.system.domain.BaseFriendsDefault;
import com.ruoyi.system.service.IAppUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BaseUserPrivileges;
import com.ruoyi.system.service.IBaseUserPrivilegesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 特权用户Controller
 *
 * @author ruoyi
 * @date 2023-03-09
 */
@RestController
@RequestMapping("/system/privileges")
public class BaseUserPrivilegesController extends BaseController {
    @Autowired
    private IBaseUserPrivilegesService baseUserPrivilegesService;
    @Autowired
    private IAppUserService iAppUserService;

    /**
     * 查询特权用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:privileges:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseUserPrivileges baseUserPrivileges) {
        startPage();
        List<BaseUserPrivileges> list = baseUserPrivilegesService.selectBaseUserPrivilegesList(baseUserPrivileges);
        return getDataTable(list);
    }

    /**
     * 导出特权用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:privileges:export')")
    @Log(title = "特权用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseUserPrivileges baseUserPrivileges) {
        List<BaseUserPrivileges> list = baseUserPrivilegesService.selectBaseUserPrivilegesList(baseUserPrivileges);
        ExcelUtil<BaseUserPrivileges> util = new ExcelUtil<BaseUserPrivileges>(BaseUserPrivileges.class);
        util.exportExcel(response, list, "特权用户数据");
    }

    /**
     * 获取特权用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:privileges:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(baseUserPrivilegesService.selectBaseUserPrivilegesById(id));
    }

    /**
     * 新增特权用户
     */
    @PreAuthorize("@ss.hasPermi('system:privileges:add')")
    @Log(title = "特权用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseUserPrivileges baseUserPrivileges) {
        AppUser appUser = iAppUserService.getOne(new QueryWrapper<AppUser>().eq("account", baseUserPrivileges.getAccount()));
        if (appUser == null) {
            return error("用户不存在");
        }
        baseUserPrivileges.setNickName(appUser.getUsername());

        Long count = baseUserPrivilegesService.count(new QueryWrapper<BaseUserPrivileges>().eq("account", baseUserPrivileges.getAccount()));
        if (count > 0) {
            return error("已存在");
        }
        return toAjax(baseUserPrivilegesService.insertBaseUserPrivileges(baseUserPrivileges));
    }

    /**
     * 修改特权用户
     */
    @PreAuthorize("@ss.hasPermi('system:privileges:edit')")
    @Log(title = "特权用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseUserPrivileges baseUserPrivileges) {
        AppUser appUser = iAppUserService.getOne(new QueryWrapper<AppUser>().eq("account", baseUserPrivileges.getAccount()));
        if (appUser == null) {
            return error("用户不存在");
        }
        baseUserPrivileges.setNickName(appUser.getUsername());

        Long count = baseUserPrivilegesService.count(new QueryWrapper<BaseUserPrivileges>()
                .ne("id", baseUserPrivileges.getId())
                .eq("account", baseUserPrivileges.getAccount()));
        if (count > 0) {
            return error("已存在,不能重复提交");
        }
        return toAjax(baseUserPrivilegesService.updateBaseUserPrivileges(baseUserPrivileges));
    }

    /**
     * 删除特权用户
     */
    @PreAuthorize("@ss.hasPermi('system:privileges:remove')")
    @Log(title = "特权用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(baseUserPrivilegesService.deleteBaseUserPrivilegesByIds(ids));
    }
}
